package com.lifang.imsoa.facadeimpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import com.lifang.imsoa.dao.miles.read.CusGuestReadMapper;
import com.lifang.imsoa.entity.miles.CusGuest;
import com.lifang.imsoa.facade.ImUserFacadeService;
import com.lifang.imsoa.facade.UserInitRegFacadeService;
import com.lifang.imsoa.model.request.AppNameEnum;
import com.lifang.imsoa.model.response.BusinessException;
import com.lifang.imsoa.model.response.ErrorEnum;
import com.lifang.imsoa.model.response.HxUserEntity;
import com.lifang.imsoa.model.response.InitRegResponse;
import com.lifang.imsoa.util.ResponseUtil;
import com.lifang.model.Response;

@Slf4j
public class UserInitRegFacadeServiceImpl implements UserInitRegFacadeService {

	@Autowired
	private CusGuestReadMapper  cusGuestReadMapper;
	@Autowired
	private ImUserFacadeService imUserFacadeService;
	
	@Override
	public Response<InitRegResponse> regUser(AppNameEnum appNameEnum) {
		if(appNameEnum.equals(AppNameEnum.WKZF)){
			int sum = cusGuestReadMapper.selectListByPagesCount();//需要注册的总数量
			int sumLimit = 0;//总页数
			if(sum%50==0){
				sumLimit = sum/50;
			}else{
				sumLimit = sum/50+1;
			}
			int registry = 0;//总注册个数
			Long sumMillSec = 0l;//注册总耗时
			List<Response> exCount = new ArrayList<Response>();
			List<Response> errorCount = new ArrayList<Response>();
			for(int i=1;i<=sumLimit;i++){
				Map<String,Object> param = new HashMap<String, Object>();
				getParamByPage(param, i, 50);
				List<CusGuest> users = cusGuestReadMapper.selectListByPages(param);
				List<String> imUsers = new ArrayList<String>();
				for(CusGuest user:users){
					imUsers.add(user.getId()+"");
				}
				long oneTimeStartTime = System.currentTimeMillis();
				Response<List<HxUserEntity>> registerResult = imUserFacadeService.xCreateNewIMUserBatchGen(imUsers,AppNameEnum.WKZF);
				int sucCountPerTime = 0;//每次成功个数
				if(registerResult.getStatus()==1){
					List<HxUserEntity> hxUsers = registerResult.getData();
					if(hxUsers!=null){
						sucCountPerTime = hxUsers.size();
					}
				}else{
					if("用户名已存在".equals(registerResult.getMessage())){
						for(CusGuest user:users){
							Response<HxUserEntity> singleRes = imUserFacadeService.xCreateNewIMUserSingleForSys(user.getId()+"", AppNameEnum.WKZF);
							if(singleRes.getStatus()==1){
								sucCountPerTime += 1;
							}else{
								errorCount.add(singleRes);
								if(!"用户名已存在".equals(singleRes.getMessage())){
									log.info("单个注册异常情况={}",singleRes.getMessage());
								}
							}
						}
					}else{
						exCount.add(registerResult);
						log.info("总异常情况={}",registerResult);
					}
							
				}
				Long oneTimeMillSec = System.currentTimeMillis()-oneTimeStartTime;
				Long oneTimeSecond = oneTimeMillSec/1000;
				log.info("第{}页成功注册个数={},耗时={}秒,{}毫秒",i,sucCountPerTime, 
						oneTimeSecond.intValue(), oneTimeMillSec);
				//统计总成功个数和总时间
				registry += sucCountPerTime;
				sumMillSec += oneTimeMillSec;
			}
			Long sumSec = sumMillSec/1000;
			log.info("注册账户总个数={},耗时={}秒,{}毫秒",registry,sumSec.intValue(), sumMillSec);
			
			InitRegResponse initRegResponse = new InitRegResponse();
			initRegResponse.setRegSum(registry);
			initRegResponse.setSumSec(sumSec.intValue());
			initRegResponse.setSumMillSec(sumMillSec);
			initRegResponse.setErrorCount(errorCount.size());
			initRegResponse.setExCount(exCount.size());
			return ResponseUtil.getResultToResponse(initRegResponse);
		}else{
			throw new BusinessException(ErrorEnum.SOA_ERROR_200007);
		}
		
	}

	public void getParamByPage(Map<String,Object> param,int page,int rowPer){
		param.put("start", (page-1)*rowPer);
		param.put("end", rowPer);
	}
}
